{% extends "base.html" %}
{% load i18n %}
{% load event_tags %}
{% load display_tags %}

{% block content %}
    {{ block.super }}
    <h2> {{ name }} <i class="fa-solid fa-circle-question has-popover" data-trigger="hover"
                       data-content="{% blocktrans %}This simple search function will return results whose findings or finding templates
                                     title, URL, description, endpoints, tags, references, languages or technologies contain the search query and products whose
                                     name, tags or description contain the search query.</br> Advanced search operators: (Restrict results to a certain type) product:,
                                     engagement:, finding:, endpoint:, tag:, language:, technology: or vulnerability_id:.
                                     test-tags shows findings in tests that are tagged with provided tag, similar for engagement-tags and product-tags.
                                     After submitting the search query, tabbed results will be displayed. The findings tab will have the possibility to
                                     to perform more finegrained filtering on status fields, test type, etc.{% endblocktrans %}"
                       data-placement="right" data-container="body" data-original-title="" title="">
                    </i>
        </h2>

    <div class="custom-search-form2">
        <form role="search" method="get" action="{% url 'simple_search' %}">
            <div class="input-group">
                <input id="custom_search_query" type="text" name="query" class="form-control"
                        placeholder="{% trans "Search" %}..." value="{{clean_query}}">
                <span class="input-group-btn">
                    <button class="btn btn-primary" type="submit" aria-label="Search">
                        <i class="fa-solid fa-magnifying-glass"></i>
                    </button>
                </span>
            </div>
        </form>
    </div>

    <div>
        <p></p>
    </div>

    <div role="tabpanel">

        <!-- Nav tabs -->
        <ul class="nav nav-tabs" role="tablist">
            {% if findings %}
                <li role="presentation" {% if activetab == 'findings' %}class="active"{% endif %}>
                    <a href="#tabs-1" role="tab" data-toggle="tab">{% trans "Findings" %} ({{ findings.paginator.count }})</a>
                </li>
            {% endif %}
            {% if vulnerability_ids %}
                <li role="presentation" {% if activetab == 'vulnerability_ids' %}class="active"{% endif %}>
                    <a href="#tabs-2" role="tab" data-toggle="tab">{% trans "Vulnerability Ids" %} ({{ vulnerability_ids|length }})</a>
                </li>
            {% endif %}
            {% if products %}
                <li role="presentation" {% if activetab == 'products' %}class="active"{% endif %}>
                    <a href="#tabs-3" role="tab" data-toggle="tab">{% trans "Products" %} ({{ products|length }})</a>
                </li>
            {% endif %}
            {% if engagements %}
                <li role="presentation" {% if activetab == 'engagements' %}class="active"{% endif %}>
                    <a href="#tabs-4" role="tab" data-toggle="tab">{% trans "Engagements" %} ({{ engagements|length }})</a>
                </li>
            {% endif %}
            {% if tests %}
                <li role="presentation" {% if activetab == 'tests' %}class="active"{% endif %}>
                    <a href="#tabs-5" role="tab" data-toggle="tab">{% trans "Tests" %} ({{ tests|length }})</a>
                </li>
            {% endif %}
            {% if endpoints %}
                <li role="presentation" {% if activetab == 'endpoints' %}class="active"{% endif %}>
                    <a href="#tabs-6" role="tab" data-toggle="tab">{% trans "Endpoints" %} ({{ endpoints|length }})</a>
                </li>
            {% endif %}
            {% if finding_templates %}
                <li role="presentation">
                    <a href="#tabs-7" role="tab" data-toggle="tab">{% trans "Finding Templates" %} ({{ finding_templates|length }})</a>
                </li>
            {% endif %}
            {% if languages %}
                <li role="presentation">
                    <a href="#tabs-8" role="tab" data-toggle="tab">{% trans "Languages" %} ({{ languages|length }})</a>
                </li>
            {% endif %}
            {% if app_analysis %}
                <li role="presentation">
                    <a href="#tabs-9" role="tab" data-toggle="tab">{% trans "Application Technolgies" %} ({{ app_analysis|length }})</a>
                </li>
            {% endif %}
            {% if tagged_findings or tagged_finding_templates or tagged_products or tagged_tests or tagged_endpoints or tagged_engagements %}
                <li role="presentation" {% if activetab == 'tagged' %}class="active"{% endif %}>
                    <a href="#tabs-10" role="tab" data-toggle="tab">Tagged
                        ({{ tagged_findings|length }}, {{ tagged_products|length }}, {{ tagged_engagements|length }}, {{ tagged_tests|length }}, {{ tagged_endpoints|length }},
                        {{ tagged_finding_templates|length }})
                    </a>
                </li>
            {% endif %}
            {% if generic %}
                <li role="presentation" {% if activetab == 'generic' %}class="active"{% endif %}>
                    <a href="#tabs-11" role="tab" data-toggle="tab">Experimental ({{ generic|length }})</a>
                </li>
            {% endif %}
        </ul>
        <!-- Tab panes -->
        <div class="tab-content">
            {% if findings %}
                <div role="tabpanel" class="table-responsive tab-pane {% if activetab == 'findings' %}active{% endif %}" id="tabs-1">
                    {% comment %} include inherits the current context so findings, filtered and other variables {% endcomment %}
                    {% include "dojo/findings_list_snippet.html" with form_id="custom_search_filter" %}
                </div>
            {% endif %}
            {% if vulnerability_ids %}
                <div role="tabpanel" class="table-responsive tab-pane  {% if activetab == 'vulnerability_ids' %}active{% endif %}" id="tabs-2">
                    <table class="table table-striped">
                        <thead>
                        <tr>
                            <th>{% trans "Vulnerability Id" %}</th>
                            <th>{% trans "EPSS Score / Percentile" %}</th>
                            <th>{% trans "Product" %}</th>
                            <th>{% trans "Engagement" %}</th>
                            <th>{% trans "Test" %}</th>
                            <th>{% trans "Finding" %}</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for vulnerability_id in vulnerability_ids %}
                            {% if vulnerability_id.id %}
                            <tr>
                                <td>
                                    <a class="search-finding" href="{% url 'view_finding' vulnerability_id.finding.id %}">
                                        {{ vulnerability_id.vulnerability_id }}</a>
                                </td>
                                <td>
                                    {{ vulnerability_id.finding.epss_score|format_epss }}
                                    /
                                    {{ vulnerability_id.finding.epss_percentile|format_epss }}
                                </td>
                                <td>
                                    <a href="{% url 'view_product' vulnerability_id.finding.test.engagement.product.id %}">
                                        {{ vulnerability_id.finding.test.engagement.product.name }}</a>
                                    {% include "dojo/snippets/tags.html" with tags=vulnerability_id.finding.test.engagement.product.tags.all %}
                                </td>
                                <td>
                                    <a href="{% url 'view_engagement' vulnerability_id.finding.test.engagement.id %}">
                                        {{ vulnerability_id.finding.test.engagement.name }}</a>
                                    {% include "dojo/snippets/tags.html" with tags=vulnerability_id.finding.test.engagement.tags.all %}
                                </td>
                                <td>
                                    <a href="{% url 'view_test' vulnerability_id.finding.test.id %}">
                                        {{ vulnerability_id.finding.test }}</a>
                                    {% include "dojo/snippets/tags.html" with tags=vulnerability_id.finding.test.tags.all %}
                                </td>
                                <td>
                                    <a href="{% url 'view_finding' vulnerability_id.finding.id %}">
                                        {{ vulnerability_id.finding.title }}</a>
                                    {% include "dojo/snippets/tags.html" with tags=vulnerability_id.finding.tags.all %}
                                </td>
                            </tr>
                            {% endif %}
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
            {% endif %}
            {% if products %}
                <div role="tabpanel" class="table-responsive tab-pane  {% if activetab == 'products' %}active{% endif %}" id="tabs-3">
                    <table class="table table-striped">
                        <thead>
                        <tr>
                            <th>{% trans "Name" %}</th>
                            <th>{% trans "Description" %}</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for product in products %}
                            {% comment %} sometimes the index contains references to findings that no longer exist. even after rebuilding the index. {% endcomment %}
                            {% if product.id %}
                            <tr>
                                <td>
                                    <a class="search-finding" href="{% url 'view_product' product.id %}">{{ product.name }}</a>
                                    {% include "dojo/snippets/tags.html" with tags=product.tags.all %}
                                </td>
                                <td>{{ product.description|truncatechars_html:150|markdown_render }}</td>
                            </tr>
                            {% endif %}
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
            {% endif %}
            {% if engagements %}
                <div role="tabpanel" class="table-responsive tab-pane {% if activetab == 'engagements' %}active{% endif %}" id="tabs-4">
                    <table class="table table-striped">
                        <thead>
                        <tr>
                            <th>{% trans "Type" %}</th>
                            <th>{% trans "Product" %}</th>
                            <th>{% trans "Date" %}</th>
                            <th>{% trans "Status" %}</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for engagement in engagements %}
                            {% comment %} sometimes the index contains references to findings that no longer exist. even after rebuilding the index. {% endcomment %}
                            {% if engagement.id %}
                            <tr>
                                <td>
                                    <a class="search-finding" href="{% url 'view_engagement' engagement.id %}">{{ engagement.name }}</a>
                                    {% include "dojo/snippets/tags.html" with tags=engagement.tags.all %}
                                </td>
                                <td>
                                    <a href="{% url 'view_product' engagement.product.id %}">{{ engagement.product.name }}</a>
                                    {% include "dojo/snippets/tags.html" with tags=engagement.product.tags.all %}
                                </td>
                                <td>{{ engagement.target_start|date }} - {{ engagement.target_end|date }}</td>
                                <td>{{ engagement.status }}</td>
                            </tr>
                            {% endif %}
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
            {% endif %}
            {% if tests %}
                <div role="tabpanel" class="table-responsive tab-pane  {% if activetab == 'tests' %}active{% endif %}" id="tabs-5">
                    <table class="table table-striped">
                        <thead>
                        <tr>
                            <th>{% trans "Title" %}</th>
                            <th>{% trans "Product" %}</th>
                            <th>{% trans "Engagement" %}</th>
                            <th>{% trans "Date" %}</th>
                            <th>{% trans "Status" %}</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for test in tests %}
                            {% comment %} sometimes the index contains references to findings that no longer exist. even after rebuilding the index. {% endcomment %}
                            {% if test.id %}
                            <tr>
                                <td>
                                    <a class="search-finding" href="{% url 'view_test' test.id %}">{{ test }}</a>
                                    {% include "dojo/snippets/tags.html" with tags=test.tags.all %}
                                </td>
                                <td>
                                    <a href="{% url 'view_product' test.engagement.product.id %}">{{ test.engagement.product.name }}</a>
                                    {% include "dojo/snippets/tags.html" with tags=test.engagement.product.tags.all %}
                                </td>
                                <td>
                                    <a href="{% url 'view_engagement' test.engagement.id %}">{{ test.engagement.name }}</a>
                                    {% include "dojo/snippets/tags.html" with tags=test.engagement.tags.all %}
                                </td>
                                <td>{{ test.engagement.target_start|date }} - {{ test.engagement.target_end|date }}</td>
                                <td>{{ test.engagement.status }}</td>
                            </tr>
                            {% endif %}
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
            {% endif %}
            {% if endpoints %}
                <div role="tabpanel" class="table-responsive tab-pane {% if activetab == 'endpoints' %}active{% endif %}" id="tabs-6">
                    <table class="table table-striped">
                        <thead>
                        <tr>
                            <th>{% trans "Endpoint" %}</th>
                            <th>{% trans "Product" %}</th>
                            <th>{% trans "Open Findings" %}</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for e in endpoints %}
                            <tr>
                                <td>
                                    <a href="{% url 'view_endpoint' e.id %}">{{ e }}{% if e.is_broken %} <span data-toggle="tooltip" title="{% trans "Endpoint is broken. Check documentation to look for fix process" %}" >&#128681;</span>{% endif %}</a>
                                    {% include "dojo/snippets/tags.html" with tags=e.tags.all %}
                                </td>
                                {% if e.product %}
                                    <td>
                                        <a href="{% url 'view_product' e.product.id %}">{{ e.product.name }}</a>
                                        {% include "dojo/snippets/tags.html" with tags=e.product.tags.all %}
                                    </td>
                                {% else %}
                                    <td>{% trans "None" %}</td>
                                {% endif %}
                                <td>
                                  {% if e.active_finding_count > 0 %}
                                  <a href="{% url 'open_findings' %}?endpoints={{ e.id }}">{{ e.active_finding_count }}</a>
                                  {% else %}
                                  {% trans "No Open, Active Findings" %}
                                  {% endif %}
                                </td>
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
            {% endif %}
            {% if finding_templates %}
                <div role="tabpanel" class="table-responsive tab-pane" id="tabs-7">
                    <table class="table table-striped">
                        <thead>
                        <tr>
                            <th>{% trans "Title" %}</th>
                            <th>{% trans "Description" %}</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for finding_template in finding_templates %}
                            <tr>
                                <td>
                                    <a class="search-finding" href="{% url 'edit_template' finding_template.id %}">{{ finding_template.title }}</a>
                                    {% include "dojo/snippets/tags.html" with tags=finding_template.tags.all %}
                                </td>
                                <td>{{ finding_template.description }}</td>
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
            {% endif %}
            {% if languages %}
            <div role="tabpanel"
                 class="table-responsive tab-pane"
                 id="tabs-8">
                    <table class="table table-striped">
                        <thead>
                        <tr>
                            <th>{% trans "Language" %}</th>
                            <th>{% trans "Product" %}</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for lang in languages %}
                            <tr>
                               <td>{{ lang.language.language }}</td>
                                <td>
                                    <a class="search-finding" href="{% url 'view_product' lang.product.id %}">{{ lang.product.name }}</a>
                                    {% include "dojo/snippets/tags.html" with tags=lang.product.tags.all %}
                                </td>
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
            {% endif %}
            {% if app_analysis %}
            <div role="tabpanel"
                 class="table-responsive tab-pane"
                 id="tabs-9">
                    <table class="table table-striped">
                        <thead>
                        <tr>
                            <th>{% trans "Technology" %}</th>
                            <th>{% trans "Product" %}</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for app in app_analysis %}
                            <tr>
                               <td>{{ app.name }}</td>
                                <td>
                                    <a class="search-finding" href="{% url 'view_product' app.product.id %}">{{ app.product.name }}</a>
                                    {% include "dojo/snippets/tags.html" with tags=app.product.tags.all %}
                                </td>
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
            {% endif %}
            {% if tagged_findings or tagged_finding_templates or tagged_products or tagged_tests or tagged_endpoints or tagged_engagements %}
                <div role="tabpanel"
                     class="table-responsive tab-pane {% if activetab == 'tagged' %}active{% endif %}"
                     id="tabs-10">
                    <table class="table table-striped">
                        <thead>
                        <tr>
                            <th>{% trans "Type" %}</th>
                            <th>{% trans "Item" %}</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% if tagged_tests %}
                            {% for test in tagged_tests %}
                                <tr>
                                    <td>{% trans "Test" %}</td>
                                    <td>
                                        <a class="search-finding" href="{% url 'view_test' test.id %}">{{ test }}</a>
                                        {% include "dojo/snippets/tags.html" with tags=test.tags.all %}
                                    </td>
                                </tr>
                            {% endfor %}
                        {% endif %}
                        {% for finding in tagged_findings %}
                            <tr>
                                <td>{% trans "Finding" %}</td>
                                <td><a class="search-finding"
                                       href="{% url 'view_finding' finding.id %}">{{ finding.title }}</a>
                                </td>
                            </tr>
                        {% endfor %}
                        {% for finding_template in tagged_finding_templates %}
                            <tr>
                                <td>{% trans "Finding Template" %}</td>
                                <td>
                                    <a class="search-finding" href="{% url 'edit_template' finding_template.id %}">{{ finding_template.title }}</a>
                                    {% include "dojo/snippets/tags.html" with tags=finding_template.tags.all %}
                                </td>
                            </tr>
                        {% endfor %}
                        {% for product in tagged_products %}
                            <tr>
                                <td>{% trans "Product" %}</td>
                                <td>
                                    <a class="search-finding" href="{% url 'view_product' product.id %}">{{ product.name }}</a>
                                    {% include "dojo/snippets/tags.html" with tags=product.tags.all %}
                                </td>
                            </tr>
                        {% endfor %}
                        {% for endpoint in tagged_endpoints %}
                            <tr>
                                <td>{% trans "Endpoint" %}</td>
                                <td>
                                    <a class="search-finding" href="{% url 'view_endpoint' endpoint.id %}">{{ endpoint }}{% if endpoint.is_broken %} <span data-toggle="tooltip" title="Endpoint is broken. Check documentation to look for fix process" >&#128681;</span>{% endif %}</a>
                                    {% include "dojo/snippets/tags.html" with tags=endpoint.tags.all %}
                                </td>
                            </tr>
                        {% endfor %}
                        {% for eng in tagged_engagements %}
                            <tr>
                                <td>{% trans "Engagement" %}</td>
                                <td>
                                    <a class="search-finding" href="{% url 'view_engagement' eng.id %}">{{ eng }}</a>
                                    {% include "dojo/snippets/tags.html" with tags=eng.tags.all %}
                                </td>
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
            {% endif %}
            {% if generic %}
                <div role="tabpanel" class="table-responsive tab-pane {% if activetab == 'generic' %}active{% endif %}"
                     id="tabs-11">
                    <table class="table table-striped">
                        <thead>
                        <tr>
                            <th>{% trans "ID" %}</th>
                            <th>{% trans "Type" %}</th>
                            <th>{% trans "Product" %}</th>
                            <th>{% trans "Severity" %}</th>
                            <th>{% trans "Title" %}</th>
                            <th>{% trans "Description" %}</th>
                            <th>{% trans "JIRA" %}</th>
                            <th>{% trans "Rank" %}</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for result in generic %}
                            {% comment %} sometimes the index contains references to findings that no longer exist. even after rebuilding the index. {% endcomment %}
                            {% if result.object.id %}
                                <tr>
                                    <td>
                                        <a href="{{result.url}}" title="{{result.title}}">
                                            <span>
                                                {{ result.object.id }}
                                            </span>
                                        </a>
                                    </td>
                                    <td>
                                        <a href="{{result.url}}" title="{{result.title}}">
                                            {{ result.object| class_name }}
                                        </a>
                                    </td>
                                    <td>
                                        <a href="{{result.url}}" title="{{result.title}}">
                                            {% comment %} a{{ result.meta.engagement__product__name }}b {% endcomment %}
                                            {% firstof result.meta.finding__test__engagement__product__name result.meta.test__engagement__product__name result.meta.engagement__product__name result.meta.product__name result.object.name %}
                                        </a>
                                    </td>
                                    <td class="centered">
                                        {% if result.meta.severity_display %}
                                        <a href="{{result.url}}" title="{{result.title}}">
                                                <span class="label severity severity-{{ result.meta.severity }}">
                                                    {{ result.meta.severity_display }}
                                                </span>
                                        </a>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <a href="{{result.url}}" title="{{result.title}}">
                                            {{ result.title }}
                                        </a>
                                    </td>
                                    <td width="50%">
                                        {% comment %} {{ result.content }} {% endcomment %}
                                        <span>
                                            <p>{{ result.object.description }}</p>
                                            <p>{{ result.object.impact }}</p>
                                            <p>{{ result.object.mitigation }}</p>
                                        </span>
                                    </td>
                                    <td>
                                        <a href="{{result.url}}" title="{{result.title}}">
                                            <span style="white-space: nowrap;">{{ result.meta.jira_issue__jira_key }}</span>
                                        </a>
                                    </td>
                                    <td>
                                        {{ result.watson_rank }}
                                    </td>
                                </tr>
                            {% endif %}
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
            {% endif %}
        </div>
    </div>

{% endblock %}
{% block postscript %}
    {{ block.super }}
    <script type="text/javascript">
        {% comment %} django filters do no support a "full_text" query field which is not a field of the model
        so we have to inject it clientside to qavoid duplicating the whole form with hardcoded fields etc. {% endcomment %}
        $(function () {
            set_custom_search_filter_handlers();
        });

        function set_custom_search_filter_handlers() {
            var query = $('#custom_search_query').val()
            var custom_search_filter = $('#custom_search_filter')
            {% comment %} alert(custom_search_filter.html())             {% endcomment %}
            var apply = custom_search_filter.find("#apply")
            var clear = custom_search_filter.find("#clear")
            {% comment %} alert(apply.html())             {% endcomment %}
            {% comment %} alert(clear.html())             {% endcomment %}
            apply.click(submit_fulltext_query)
            clear.click(goto_fulltext_query)
        }

        function submit_fulltext_query() {
            var query = $('#custom_search_query').val()
            var custom_search_filter = $('#custom_search_filter')
            $('<input>').attr({ type: 'hidden',
                                id: 'query',
                                name: 'query',
                                value: query
                             }).appendTo(custom_search_filter);
        }

        function goto_fulltext_query() {
            var query = $('#custom_search_query').val()
            var custom_search_filter = $('#custom_search_filter')
            var clear = custom_search_filter.find("#clear")
            clear.attr('href', '?query=' + query)

        }


    </script>
{% endblock %}
